# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libxslt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.37
pkgrel=2
pkgdesc="XML stylesheet transformation library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://gitlab.gnome.org/GNOME/libxslt/-/wikis/home"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libxml2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
options=('strip' 'staticlibs')
source=("https://download.gnome.org/sources/libxslt/${pkgver%.*}/${_realname}-${pkgver}.tar.xz"
        '0003-fix-concurrent-directory-creation.all.patch'
        '0010-xslt-config-win-paths.patch')
sha256sums=('3a4b27dc8027ccd6146725950336f1ec520928f320f144eb5fa7990ae6123ab4'
            'fc23bd3c9e62a28cb1c020cebc83ca7532a5a7a21c2e4748b72d13ac7716a851'
            '9e7a75d479e5cd799b52ccfe5762c18f4d9af0dc687fab257ef2dec6e71a08f1')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ] || [ -d $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    "0003-fix-concurrent-directory-creation.all.patch" \
    "0010-xslt-config-win-paths.patch"

  autoreconf -fi
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  CPPFLAGS+=" -DLIBXML_STATIC -DLIBEXSLT_STATIC -DLIBXSLT_STATIC" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-shared \
    --enable-static \
    --with-crypto \
    --with-plugins \
    --with-libxml-prefix=${MINGW_PREFIX} \
    --without-python

  make

  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

 ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-crypto \
    --with-plugins \
    --with-libxml-prefix=${MINGW_PREFIX} \
    --without-python

  make
}

package() {
  export MSYS2_ARG_CONV_EXCL="-//OASIS"

  cd "${srcdir}/build-${MSYSTEM}-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}-shared"
  make DESTDIR="${pkgdir}" install

  # Remove hard coded paths
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}\/include|\${includedir}|g" -i "${pkgdir}"${MINGW_PREFIX}/bin/xslt-config
  sed -s "s|${PREFIX_DEPS}\/lib|\${libdir}|g" -i "${pkgdir}"${MINGW_PREFIX}/bin/xslt-config
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/xsltConf.sh
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libexslt.pc
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/pkgconfig/libxslt.pc
}
